#!/usr/bin/php -q
<?php
echo "http://code.google.com/p/vulnerability-check/\n";
echo "(C) 2013 Adam Ziaja <adam@adamziaja.com> http://adamziaja.com\n";
$vfeed = $argv[1];
$dpe = $argv[2];
$nmap = $argv[3];
$db = new SQLite3($vfeed);
$xml_dpe = new SimpleXMLElement(file_get_contents($dpe));
$xml_dpe->registerXPathNamespace('_', 'http://dpe.toolswatch.org/dictionary/0.2');
$xml_nmap = new SimpleXMLElement(file_get_contents($nmap));
foreach ($xml_nmap->host as $host) {
    $cpe_array = array();
    echo "\n\033[1;32m" . $host->address{'addr'} . "\033[0m\n";
    foreach ($host->ports->port as $port) {
        //$cpe = (string) $port->service->cpe;
        foreach ($port->service->cpe as $cpe) {
            $cpe = (string) $cpe;
            array_push($cpe_array, $cpe);
        }
    }
    foreach (array_filter(array_unique($cpe_array)) as $cpe) {
        echo "\033[1;34m$cpe\033[0m\n";
        $results = $db->query("SELECT cveid FROM cve_cpe WHERE cpeid = '$cpe'");
        while ($row = $results->fetchArray()) {
            $cve = $row['cveid'];
            echo "\033[1;31mhttps://web.nvd.nist.gov/view/vuln/detail?vulnId=$cve\033[0m\n";
            $cve_results = $db->query("SELECT summary FROM nvd_db WHERE cveid = '$cve'");
            while ($cve_row = $cve_results->fetchArray()) {
                echo $cve_row['summary'] . "\n";
            }
        }
        $passwd_array = array();
        $found_dpe = $xml_dpe->xpath("//_:vendor/_:model[@cpe='$cpe']/_:credential");
        foreach ($found_dpe as $element_dpe) {
            $username = (string) $element_dpe['username'];
            $password = (string) $element_dpe['password'];
            array_push($passwd_array, "username=$username password=$password");
        }
        foreach (array_filter(array_unique($passwd_array)) as $passwd) {
            echo "\033[1;35m$passwd\033[0m\n";
        }
    }
}
?>
